Libraries

There is a problem with dplyr and plotly. When loaded tofether both have functions of same name so must point to the library you reall need, i.e. dplyr::select()

#library(reticulate)
library(tidyverse)
library(plotly)
library(janitor)
library(readxl)

Download the DK dataset for today

    Remove the players that are out or on the injured reserve
player_projection <- read_csv("DFF_NHL_cheatsheet.csv", col_names = TRUE) %>%
 dplyr::filter(!injury_status %in% c("O", "IR")) %>% # remove the out and injured reserve players
 dplyr::mutate(salary_factor = salary*.001, range_factor = (L5_ppg_max-L5_ppg_floor)/2, total_score = salary_factor + range_factor + ppg_projection + value_projection) 

Select a line based on max ppg totals

Clean Up and filtering

      slice takes the top 3 grouped records
      later I ungroup and combine field names.
      
      Then I filter out points above 60 to create a new object for graphing
team__reg_line <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(!position == "G") %>%
  filter(!reg_line  == 0) %>%
  select(team, reg_line, total_score, salary) %>%
  arrange(team, reg_line, desc(total_score)) %>%
  group_by(team, reg_line) %>%
  dplyr::slice(1:3) %>%
  dplyr::summarise(total_pts = sum(total_score), salary = sum(salary)) %>%
  dplyr::ungroup() %>%
  unite(team_line, team, reg_line, sep = "_", remove = TRUE) 
## `summarise()` has grouped output by 'team'. You can override using the
## `.groups` argument.
team_pts_max_20 <- team__reg_line %>%
  head(20)

Use ggplot to graph

    this is called a lollypop chart
# install.packages("ggplot2")
library(ggplot2)

ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
  geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = round(total_pts)), color = "white", size = 3) 

## Use plotly with ggplot2

p <- ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
  geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = round(total_pts)), color = "white", size = 3) 

ggplotly(p)

GOALIE SECTION

Start Working on selecting a Goalie

Review choices for goalie bases on DK Spreadsheet
      Filter for Goalies only
      Reduce data elements
      sort descending value
      create a variance/spread column replacing l5_
      create new field names for connecting to other databases 
goalies <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(position == "G") %>%
  select(1:3, 6, 8, 10:16, 18:20, 23:25) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)


goalie_select_10 <- goalies %>%
  head(10)

Create a chart that looks at 10 top value score selections based on value scores

# install.packages("ggplot2")
library(ggplot2)

ggplot(goalie_select_10, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 5000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +

geom_text(aes(label = total_score), color = "white", size = 3) 

Create the Goalie Selection chart in plotly

p <- ggplot(goalie_select_10, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 5000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 


ggplotly(p)

CENTER SELECTION

Now find some centers

centers <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(position == "C") %>%
  select(1:3, 6, 8, 10:16, 18:20, 23:25) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)

center_select_15 <- centers %>%
  head(15)

Create a chart that looks at 15 top value score selections based on value scores

# install.packages("ggplot2")
library(ggplot2)

ggplot(center_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 

Create the Center Selection chart in plotly

p <- ggplot(center_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 


ggplotly(p)

WINGS SELTECTION

Now find some wingmen

wings <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(position == "W") %>%
  select(1:3, 6, 8, 10:16, 18:20, 23:25) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)


wing_select_15 <- wings %>%
  head(15)

Create a chart that looks at 15 top value score selections

# install.packages("ggplot2")
library(ggplot2)

ggplot(wing_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 

## Create the wing Selection chart in plotly

p <- ggplot(wing_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 


ggplotly(p)

DEFENSEMEN SELECTION

Now find some defensemen based on value scores

defense <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(position == "D") %>%
  select(1:3, 6, 8, 10:16, 18:20, 23:25) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)

defense_select_15 <- defense %>%
  head(15)

Create a chart that looks at 15 top value score selections

# install.packages("ggplot2")
library(ggplot2)

ggplot(defense_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 

## Create the wing Selection chart in plotly

p <- ggplot(defense_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 


ggplotly(p)

Reengineer Austin’s 4th Place

austin <- player_projection %>%
  dplyr::filter(last_name %in% c("Barzal", "Hughes", "Bratt", "Hulua", "Wahlstrom", "Martinez", "Makar", "Kallgren", "Holmstrom")) %>%
clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  select(1:3, 6, 8, 10:16, 18:20, 23:25) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)

Austin Plots looking at value scores

ggplot(austin, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 

Austin Plots based on max ppg

ggplot(austin, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = round(total_score)), color = "white", size = 3) 

Reengineer Mark’s Pick

mark <- player_projection %>%
  dplyr::filter(last_name %in% c("Hischer", "Seguin", "Bratt", "Marchessault", "Smith", "Hamilton", "Theodore", "DeSmith", "Carrier")) %>%
clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  select(1:3, 6, 8, 10:16, 18:20, 23:25) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)

##  Mark's  Plots looking at value scores
ggplot(mark, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 

Mark’s Plots based on max ppg

ggplot(mark, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = round(total_score)), color = "white", size = 3) 

Combine player data frames

combo_player <- do.call("rbind", list(goalies, centers, wings, defense))

write_csv(combo_player, "combo_player.csv")